<?php

namespace Admin\Controller;

use Think\Controller;

class BaseController extends Controller {

  var $adminId, $adminInfo, $adminMenu, $shopConfig;
  var $menuSelectClass = 'active';
  var $notcheck = array('login', 'ajaxGetRegion','findPwd','addmoney');


  public function __construct() {
    parent::__construct();
   /* if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') > 0 ) {
        header('location:'.U('Wap/Index/index'));
    }*/
    $this->checkLogin();
    $this->getAdminMenu();
    $this->loadConfig();
    $sysname = M("Sys")->where(array("name"=>"sys_name"))->getField("value");
    $this->assign('sysname', $sysname);
    $this->assign('m', MODULE_NAME);
    $this->assign('c', CONTROLLER_NAME);
    $this->assign('a', ACTION_NAME);
  }

  /**
   * 检测是否登录
   */
  public function checkLogin() {
    $adminId = session('adminId');
    if (empty($adminId) && !in_array(ACTION_NAME, $this->notcheck)) {
      $this->redirect('Index/login');
    }
    $list = D('Admin')->adminInfo(array('id' => $adminId));
    $this->adminId = $list['id'];
    $this->adminInfo = $list;
    $this->assign('loginInfo', $this->adminInfo);
  }

  /**
   * 获取后台菜单
   */
  private function getAdminMenu() {
    $admin = M("Admin");
    $adminId = session('adminId');

    if ($this->adminInfo['is_area'] == 1) {
      $this->adminMenu = C('adminAreaMenu');
    } else {
      $this->adminMenu = C('adminMenu');
    }
    foreach ($this->adminMenu as $key => $item) {
      $this->adminMenu[$key]['url'] = U($item['url']);
      if ($key == CONTROLLER_NAME) { //. '_' . ACTION_NAME
        $this->adminMenu[$key]['class'] = $this->menuSelectClass;
      }
    }

    $this->assign('menu', $this->adminMenu);
    return $this->adminMenu;
  }





  /**
   * 批量处理路由
   */
  public function batchProcess() {
    $id = I("id");
    if (empty($id)) {
      $this->error('请选择需要操作的信息.');
    }
    $batchAction = I("batch");
    $actionName = reset(array_keys($batchAction));
    if (method_exists($this, $actionName)) {
      call_user_func(array($this, $actionName));
    }
  }

  /**
   * 加载配置文件
   */
  private function loadConfig() {
    $shopConfigArray = C('shopConfig');
    foreach ($shopConfigArray as $key => $item) {
      $this->shopConfig[$key] = $item['value'];
    }
    $this->assign("shopConfig",$this->shopConfig);
    $this->assign("title",$this->shopConfig['shopName'].'管理后台');
  }
}
